package xml;

import java.awt.Rectangle;
import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * Switches between a java ready {@link java.awt.Rectangle Rectangle} object, and a {@link xml.RectangleType RectangleType} object needed to write to xml.
 * @author mylesc,varunr
 *
 */
public class RectangleAdapter extends XmlAdapter<RectangleType, Rectangle> {
	
	/**
	 * Marshals a java Rectangle object into a RectangleType form which is capable of being saved in xml.
	 * @param rectangle a Rectangle object to be converted into xml saveable format.
	 * @return rectType an object with all of rectangle's data ready to be saved to xml.
	 */
	public RectangleType marshal(Rectangle rectangle) throws Exception {
		RectangleType rectType = new RectangleType();
		rectType.setX(rectangle.x);
		rectType.setY(rectangle.y);
		rectType.setWidth(rectangle.width);
		rectType.setHeight(rectangle.height);

		return rectType;
	}

	/**
	 * Unmarshals the RectangleType form of a Rectangle and creates a new java Rectangle object.
	 * @param rectType an object that contains all data for a Rectangle saved in an xml file.
	 * @return arrows a Rectangle filled with data from an rectType.
	 */
	public Rectangle unmarshal(RectangleType rectType) throws Exception { 
		Rectangle rect = new Rectangle();
		rect.x = rectType.getX();
		rect.y = rectType.getY();
		rect.width = rectType.getWidth();
		rect.height = rectType.getHeight();
        
        return rect;
	}
}